<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2019-04-09
 * Time: 13:44
 */

namespace app\api\controller;


use app\service\CartService;
use app\service\GoodsService;
use app\common\model\Goods as GoodsModel;
use app\common\model\User as UserModel;
use app\common\model\GoodsType as GoodsTypeModel;
use app\common\model\ChatMsg as ChatMsgModel;

class Goods extends Base
{

    public static $goods_base_filed = 'id,name,price,shop_price,picture,picture_list,ord,stock,shop_type';

    //商品详情
    public function detail()
    {
        $goods_id = input('goods_id');
        if (empty($goods_id)) {
            $this->ajax(202, '商品ID不能为空');
        }
        $goods = GoodsService::S()->queryGoodsInfoByWhere(array('id' => $goods_id));
        $goods = self::dealGoods($goods);
        if (empty($goods)) {
            $this->ajax(202, '商品不存在');
        }
        $goods[0]['sku'] = GoodsService::S()->skuDetail($goods[0]['id']);
        $back['data'] = $goods[0];
        $back['cart_count'] = CartService::S()->countCart($this->uid);
        $detail = $goods[0];
        $detail['sale_val'] = $detail['sale_val'] + $detail['sale_real'];
        if ($detail['shop_type'] == 2) {
            $detail['max_score'] = round($detail['price'] * $detail['score_bili'], 2);
        }
        $this->ajax(200, 'success', $detail);
    }

    //处理商品
    public function dealGoods($goods)
    {
        foreach ($goods as $k => $v) {
            $goods[$k]['picture'] = tomedia($v['picture']);
            $pic_list = json_decode($v['picture_list'], true);
            foreach ($pic_list as $pk => $pv) {
                $pic_list[$pk]['path'] = tomedia($pv['path']);
            }
            $goods[$k]['picture_list'] = $pic_list;

        }
        return $goods;
    }

    /* 商品列表 */
    public function list()
    {
        $input = input();
        $where = [
            'shop_type' => 1,
            'onsale' => 1,
            'mall_id' => ['in', [0, $this->mall_id]]
        ];
        $orderby = 'mall_id asc,ord desc';
        if ($input['shop_type']) {
            $where['shop_type'] = $input['shop_type'];
            if ($input['shop_type'] == 4) {
                $orderby = 'shop_type asc,ord desc';
                $where['grade'] = array("NEQ", 4);
            }
        }

        if ($input['type_id'] > 0) {
            $where['type_id'] = $input['type_id'];
        } else if ($input['type_id'] == -1) {
            $where['is_tuijian|is_new|is_hot'] = 1;
        }
        if ($input['keyword']) {
            $where['name'] = [
                'like', "%{$input['keyword']}%"
            ];
        }
        $page = max($input['page'], 1);
        $size = 20;
        $list = GoodsModel::getPage($where, $page, $size, $orderby);
        /*  if($input['shop_type']==1 && $page==1 && $list) {
             $goods=\app\common\model\Goods::getOne(array('id'=>87));//店长套餐显示在首页
             array_unshift($list,$goods);
             $goods=\app\common\model\Goods::getOne(array('id'=>111));//店长套餐显示在首页
             array_unshift($list,$goods);
         } */

        if (!$list) {
            $this->ajax(200, 'success', []);
        }
        $list = $this->dealGoods($list);
        $new = [];
        foreach ($list as $k => $v) {
            $new[] = [
                'id' => $v['id'],
                'name' => $v['name'],
                'price' => $v['price'],
                'shop_price' => $v['shop_price'],
                'picture' => tomedia($v['picture']),
                'picture_list' => $v['picture_list'],
                'ord' => $v['ord'],
                'stock' => $v['stock'],
                'sale_val' => $v['sale_val'] + $v['sale_real']
            ];
        }
        $this->ajax(200, 'success', $new);
    }

    /* 商品分类 */
    public function typeList()
    {
        $input = input();
        $where = [
            'shop_type' => 1,
            'mall_id' => ['in', [0, $this->mall_id]]
        ];
        if ($input['shop_type']) {
            $where['shop_type'] = $input['shop_type'];
        }
        $list = GoodsTypeModel::getAll($where, 'mall_id asc,ord desc');
        if (!$list) {
            $this->ajax(200, 'success', []);
        }
        $this->ajax(200, 'success', $list);
    }

    /* 分享到聊天室 */
    public function sendToChat()
    {
        $input = input();
        if (!UserModel::isManager($this->uid)) {
            $this->ajax(202, '无权分享');
        }
        $goodsInfo = GoodsModel::getOneById($input['id']);
        $userInfo = UserModel::getOneById($this->uid);
        ChatMsgModel::create([
            'uid' => $this->uid,
            'content' => json_encode([
                'type' => 'message',
                'content' => [
                    'type' => 'goods',
                    'content' => $goodsInfo['name'],
                    'goods_id' => $goodsInfo['id'],
                    'price' => $goodsInfo['price'],
                    'picture' => tomedia($goodsInfo['picture']),
                    'uid' => $this->uid
                ]
            ]),
            'status' => 0,
            'mall_id' => $userInfo['mall_id'],
            'group_id' => UserModel::getGroupId($this->uid),
            'type' => 'goods'
        ]);
        $this->ajax(200, 'success');
    }


}